<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" :labelWidth="100" :collapse="false" @onCollapse="collapseHandle" @filterChange="handleFilterChange" />
    <FilterTable
      ref="mainTable"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :columns="tableColumns"
      columnsRef="aduitOrder"
      :datakey="'records'"
      :onRowSelectChange="handleRowSelectChange"
      :onColumnsChange="columns => (this.tableColumns = columns)"
      
      :isExportExcel2="true"
      :exportFileName="'采购单清单.xlsx'"
      :isSelectColumn= false
    >
      <!-- <template slot="controls">
        <el-button size="small" type="primary"  @click="exportClick">导出</el-button>
      </template> -->
    </FilterTable>
    <div>
      <Drawer width="100%" title="采购单明细" :visible.sync="isShowpartBuyItems" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <partBuyItems :top="top" @cancel="isShowpartBuyItems = false" />
      </Drawer>
      <!-- <BaseDialog title="服务商查询" :visible.sync="isShowSearchCustomer" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <SearchCustomer :customerNo="kehuxuanze" @close="isShowSearchCustomer = false" @addCustomerCode="confirmSearchCustomer" />
      </BaseDialog> -->
      <!-- <BasePrint ref="Salesprint" :data="SalesprintList" template="templatepartInventory" /> -->

    <BaseDialog
      :visible.sync="issearch"
      :title="'服务商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>
    </div>
  </div>
</template>

<script>
import { queryPartBuyCheck,getListForExport,exporturl} from '@/api/partsmanage/aduitOrder/index';
import { dictionary } from '@/utils/dictMixin';
import SearchCustomer from './SearchCustomer';
import { exportExcelFile } from '@/utils/index';
import partBuyItems from './partBuyItems';
import {banWord} from "@/utils/validate";
import moment from "moment";
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
export default {
  name: 'aduitOrder',
  mixins: [dictionary],
  components: {
    partBuyItems,
    ownerSeacher,
    SearchCustomer
  },
  data() {
    return {
      top: {},
      SalesprintList: {},
      cunUserList: [],
      rowData: {},
      cunPrintid: '',
      kehuxuanze: '',
      selectList: [],
      issearch: false,
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      isShowpartBuyItems: false,
      isShowSearchCustomer: false,
      prindfBtnDisabled: true,
      fetchapi: queryPartBuyCheck,
      // fetchapi: queryAduit,
      fetchParams: { xhrAbort: true }
    };
  },
  mounted() {
    // this.getDealerUser();
    // this.translateDealer();
  },
  methods: {
    seacherFun(){
        this.issearch = true
      },
      dealercloseHandler(val){
        if(val){
          this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
          this.topFilterList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerShortname;
        }
        this.issearch=false;
      },
    createTopFilterList() {
      return [
        {
            type: 'INPUT',
            label: '服务商代码',
            fieldName: 'dealerCode',
            placeholder: '请选择',
            unitRender: () => {
              return (
                <div>
                  <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
                </div>
              );
            }
          },
          {
            type: 'INPUT',
            label: '服务商简称',
            fieldName: 'dealerName',
            placeholder: '请输入',
          },
        {
          type: 'INPUT',
          label: '备件订单号',
          fieldName: 'purchaseCode',
          placeholder: '请输入...',
          rules: [{ validator: banWord, trigger: 'blur' },{ max: 20, message: '长度在20个字符以内', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '订单类型',
          // fieldName: 'purchaseType',
          fieldName: 'stockInType',
          itemList: this.createDictList('3003'),
          placeholder: '请选择...'
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'submStartDate|submEndDate',
          label: '提报日期',
          // initialValue: [
          //   moment().subtract(7,'days').format('YYYY-MM-DD'),
          //   moment().format('YYYY-MM-DD')
          // ],
          dateType: 'daterange'
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'purchaseStatus',
          itemList: this.createDictList('3002',[30021001,30021010]),
          placeholder: '请选择...'
        },

        {
          type: 'RANGE_DATE',
          fieldName: 'auditStartDate|auditEndDate',
          label: '审核日期',
          initialValue: [],
          dateType: 'daterange'
        },


      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: params => {

            return (
              <div>
                <el-button  type="text" size="mini" onClick={() => this.showItems(params.row)}>
                  查看明细
                </el-button>
              </div>
            );
          }
        },

        {
          title: '服务商代码',
          dataIndex: 'DEALER_CODE',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '服务商名称',
          dataIndex: 'DEALER_NAME',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '备件订单号',
          dataIndex: 'XSDDHM',
          filter: true,
          sorter: true,
          filterType: 'input',
          width: 185
        },

        {
          title: '订单类型',
          dataIndex: 'XSDDLX',
          filter: true,
          sorter: true,
          filterItems: this.createDictList('3003'),
          dictItems: this.createDictList('3003'),
          filterType: 'checkbox'
        },
        {
          title: '采购品种合计',
          dataIndex: 'COUNT_NUM',
          filter: true,
          sorter: true,
          filterType: 'input',
          hidden:true
        },
        {
          title: '采购数量合计',
          dataIndex: 'BSZSLS',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '订单金额合计',
          dataIndex: 'XSYSJE',
          filter: true,
          sorter: true,
          precision: 2,
          numberFormat: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'XSDDZT',
          filter: true,
          sorter: true,
          filterItems: this.createDictList('3002'),
          dictItems: this.createDictList('3002'),
          filterType: 'checkbox'
        },
        {
          title: '是否免运费',
          dataIndex: 'DDMYF',
          filter: true,
          sorter: true,
          filterItems: this.createDictList('1004'),
          dictItems: this.createDictList('1004'),
          filterType: 'checkbox'
        },

        {
          title: '提报人',
          dataIndex: 'SUBMIT_BYNAME',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '创建日期',
          dataIndex: 'CREATED_AT',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '订单提报日期',
          dataIndex: 'DDTBSJ',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '审核人',
          dataIndex: 'AUDIT_BY_NAME',
          filter: true,
          sorter: true,
          filterType: 'input',
          hidden:true
        },

        {
          title: '审核日期',
          dataIndex: 'AUDIT_TIME',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },

        {
          title: '发运方式',
          dataIndex: 'DDFYFS',
          filter: true,
          sorter: true,
          filterItems: this.createDictList('3024'),
          dictItems: this.createDictList('3024'),
          filterType: 'checkbox',
          hidden:true
        },

      ];
    },
    collapseHandle() {},
    handleFilterChange(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({}, {
        ...e,
        xhrAbort: false,
        submStartDate: e.submStartDate ? e.submStartDate.split(' ')[0]+' 00:00:00' : '',
        submEndDate: e.submEndDate ? e.submEndDate.split(' ')[0]+' 23:59:59' : '',
        auditStartDate: e.auditStartDate ? e.auditStartDate.split(' ')[0]+' 00:00:00' : '',
        auditEndDate: e.auditEndDate ? e.auditEndDate.split(' ')[0]+' 23:59:59' : '',
        // dealerCode: e.customerCode ? this.rowData.dealerCode : ''
      });
    },

    async confirmSearchCustomer(row) {
      console.log(row, 'rowCustomer');
      this.rowData = row[0];
      console.log('row[0].dealerCode', row[0].dealerCode);
      this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = row[0].dealerCode;
      this.topFilterList.find(x => x.fieldName == 'ownerName').initialValue = row[0].dealerShortname;
    },
    // async translateDealer() {
    //   let res = await byAllTranslate();
    //   if (res.resultCode == 200) {
    //     // console.log(res.data, 'res.data::');
    //     let dataList = res.data ? res.data : [];
    //     // console.log(dataList, 'dataList::');
    //     let tempList = dataList.map(x => ({
    //       value: x.dealerCode,
    //       text: x.dealerName
    //     }));
    //     // console.log(tempList, 'tempList::');
    //     this.tableColumns.find(x => x.dataIndex == 'ownerCode').dictItems = tempList;
    //   }
    // },
    handleRowSelectChange(rows) {
      this.selectList = rows;
    },
    showItems(e) {
      this.isShowpartBuyItems = true;
      this.top = e;
    },
    async exportClick() {
      let json = Object.assign({}, { CODE: 'CODE_00000266' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    // exportClick() {
    //   getListForExport(this.fetchParams).then(res => {
    //     exportExcelFile(res.data, '备件采购订单')
    //   });
    // },

  }
};
</script>
